package _Pan3D.particle
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.Program3D;
	import flash.display3D.textures.Texture;
	import flash.events.EventDispatcher;
	import flash.geom.Matrix3D;

	public class BaseParticle extends EventDispatcher
	{
		protected var _context3D:Context3D;
		
		private var _x:Number = 0;
		private var _y:Number = 0;
		private var _z:Number = 0;
		
		private var _scale:Number = 1;
		
		private var _absoluteX:Number = 0;
		private var _absoluteY:Number = 0;
		private var _absoluteZ:Number = 0;
		
		private var _end:Boolean;
		private var _beginTime:Number;
		
		protected var posMatrix:Matrix3D;
		
		private var _parent:ParticleCombine;
		protected var _callFun:Function;
		private var textureBuffer:Texture;
		protected var _program:Program3D;
		
		public function BaseParticle(context:Context3D)
		{
			this._context3D = context;
		}
		
		public function get x():Number
		{
			return _x;
		}
		
		public function set x(value:Number):void
		{
			_x = value;
			updataPos();
		}
		
		public function get y():Number
		{
			return _y;
		}
		
		public function set y(value:Number):void
		{
			_y = value;
			updataPos();
		}
		
		public function get z():Number
		{
			return _z;
		}
		
		public function set z(value:Number):void
		{
			_z = value;
			updataPos();
		}
		
		public function updataPos():void{
			if(this._parent){
				this._absoluteX = this._x + this._parent.x;
				this._absoluteY = this._y + this._parent.y;
				this._absoluteZ = this._z + this._parent.z;
			}
			updatePosMatrix();
		}
		
		public function updatePosMatrix():void{
			posMatrix.identity();
			posMatrix.prependTranslation(this._absoluteX, this._absoluteY, this._absoluteZ);
			posMatrix.prependScale(this._scale,this._scale,this._scale);
			
		}

		public function get parent():ParticleCombine
		{
			return _parent;
		}

		public function set parent(value:ParticleCombine):void
		{
			_parent = value;
		}

		public function set callFun(value:Function):void
		{
			_callFun = value;
		}

		public function get scale():Number
		{
			return _scale;
		}
 
		public function set scale(value:Number):void
		{
			_scale = value;
		}
		
		public function uploadTexture(bitmapdata:BitmapData):void{
			textureBuffer = this._context3D.createTexture(bitmapdata.width, bitmapdata.height, Context3DTextureFormat.BGRA, true);
			textureBuffer.uploadFromBitmapData(bitmapdata);
		}
		
		public function addTexture(texture : Texture,obj:Object) : void {
			this.textureBuffer = texture;
		}
		
		public function get end():Boolean
		{
			return _end;
		}

		public function set end(value:Boolean):void
		{
			_end = value;
		}
		
		public function reset():void{
			this._end = false;
		}

		public function get beginTime():Number
		{
			return _beginTime;
		}

		public function set beginTime(value:Number):void
		{
			_beginTime = value;
		}

		public function get program():Program3D
		{
			return _program;
		}

		public function set program(value:Program3D):void
		{
			_program = value;
		}

		
	}
}